package com.stoney.http;

import com.stoney.utils.UrlUtil;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Stony on 2015/11/24.
 */
public class LoadLog {
    public static void main(String[] args) throws Exception {
        loadRe();
    }
    static void loadRe()throws Exception{
        //flatfull
        String dir = LoadLog.class.getResource(".").getPath();
        List<String> urls = new ArrayList<String >();
        File log = new File(dir + "/re.txt");
        System.out.println(dir);
        if(log.exists()){
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(log),"UTF-8"));
            String line;
            while((line = reader.readLine()) != null){
                if((!line.isEmpty())){
                    String url = line.trim();
                    String baseUrl = "http://flatfull.com/themes/angulr/angular/";
                    if(!UrlUtil.isUrl(url)){
                        url = UrlUtil.absUrl(baseUrl, url);
                    }
                    urls.add(url);
                }
            }
            reader.close();
        }else{
            System.out.println("------------not file");
        }
        System.out.println(urls);
        queue.addAll(urls);
        download(urls);
//        run();
    }
    static void loadLog()throws Exception{
        //flatfull
        String dir = LoadLog.class.getResource(".").getPath();
        List<String> urls = new ArrayList<String >();
        File log = new File(dir + "/log3.txt");
        System.out.println(dir);
        if(log.exists()){
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(log),"UTF-8"));
            String line;
            while((line = reader.readLine()) != null){
                if((!line.isEmpty()) && (line.contains("http:") || line.contains("https:"))
                        && line.contains("flatfull")){
                    urls.add(line.replace("GET","").trim());
                }
            }
            reader.close();
        }else{
            System.out.println("------------not file");
        }
        System.out.println(urls);
        queue.addAll(urls);
        //download(urls);
        run();
    }
    static BlockingQueue<String> queue = new ArrayBlockingQueue<String>(3000);
    public static void run(){
        ExecutorService service = null;
        try{
            service = Executors.newFixedThreadPool(5);
            service.execute(new ProcessUrlsTask(queue));
            service.execute(new ProcessUrlsTask(queue));
            service.execute(new ProcessUrlsTask(queue));
            service.shutdown();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    static class ProcessUrlsTask implements Runnable{
        private BlockingQueue<String> queue;
        public ProcessUrlsTask(BlockingQueue<String> queue) {
            super();
            this.queue = queue;
        }
        @Override
        public void run() {
            while(true){
                try {
                    String url = queue.take();
                    download(url);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    static AtomicInteger downloadCount = new AtomicInteger(0);
    static void download(List<String> urls){
        for(String url : urls){
            download(url);
        }
    }
    static AtomicInteger index = new AtomicInteger(1);
    static void download(String url){
        downloadCount.incrementAndGet();
        String dir = "D:/Website/";
        String path = getImgPath(url);
        String name = getImgName(url);
        if(name == null || name.length() == 0){
            name = "/index_" + index.get() + ".html";
            index.incrementAndGet();
        }
        System.out.println("count = " + downloadCount.get());
        System.out.println("url = " + url);
        System.out.println("file = " + dir + path + "/" + name);
        File outDir = new File(dir + path);
        if(!outDir.exists()){
            outDir.mkdirs();
        }
        File outFile = new File(dir + path + "/" + name);

        FileOutputStream outputStream = null;
        InputStream inputStream = null;
        HttpURLConnection conn = null;
        int timeout = 20000;
        try {
            try {
                conn = (HttpURLConnection) new URL(url).openConnection();
                conn.setConnectTimeout(timeout);
                conn.setReadTimeout(timeout);
                conn.setRequestMethod("GET");
                conn.connect();
                System.out.println("|->interfaceCount code = " + conn.getResponseCode() + " msg = " + conn.getResponseMessage());
                if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
                    outputStream = new FileOutputStream(outFile);
                    inputStream = conn.getInputStream();
                    byte[] buffer = new byte[1024 * 4];
                    while (true) {
                        int ins = inputStream.read(buffer);
                        if (ins == -1) {
                            break;
                        } else {
                            outputStream.write(buffer, 0, ins);
                        }
                    }
                }
            } finally {
                if (conn != null) {
                    conn.disconnect();
                }
                if(outputStream != null ) outputStream.close();
                if (inputStream != null) {
                    inputStream.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    static String getImgPath(String url) {
        //System.out.println("getImgPath : " + url);
        if (url.startsWith("http:")) {
            url = url.substring(url.indexOf("//") + 1);
            url = url.substring(url.indexOf("/") + 1);
            url = url.substring(0, url.lastIndexOf("/"));
            return url;
        } else {
            return null;
        }
    }

    static String getImgName(String url) {
        //System.out.println("getImgName : " + url);
        url = url.substring(url.lastIndexOf("/") + 1);
        if(url.lastIndexOf("?") != -1 ){
            url = url.substring(0, url.lastIndexOf("?"));
        }
        return url;
    }
}
